import {
  KKConversationFrom,
  KKConversationSource,
  KKConversationType,
  KKConversationStatus,
} from "@/pagesA/answer/components/conversitonEnum";
import { isJSON } from "@/utils/util";

// 获取聊天来源
export const getChatFrom = (data: any) => {
  // type 对话类型1：提问,2：回答，3：追问
  if (data.type === 2) {
    return KKConversationFrom.me;
  }
  return KKConversationFrom.other;
};

// 获取聊天来源
export const getChatSource = (data: any) => {
  //contentFlag 内容标识 1：人工，2：系统
  if (data.contentFlag === 1) {
    return KKConversationSource.artificial;
  } else if (data.contentFlag === 2) {
    return KKConversationSource.system;
  }
  return KKConversationSource.artificial;
};

// 获取聊天时间
export const getChatTime = (
  data: any,
  showFullYear = false,
  yearSplit = "."
) => {
  if (!data.createdAt) {
    return "";
  }
  const now = new Date();
  const currentYear = now.getFullYear();
  let timeArr = data.createdAt.split(".");
  if (timeArr.length < 2) {
    timeArr = data.createdAt.split("-");
  }
  const timeYear = timeArr.length > 0 ? timeArr[0] : "";
  if (Number(timeYear) !== currentYear) {
    return data.createdAt;
  } else {
  }
  let isShowYear = false;
  if (showFullYear || Number(timeYear) !== currentYear) {
    isShowYear = true;
  }
  let timrStr = "";
  timeArr.forEach((item: any, index: number) => {
    if (isShowYear) {
      if (index >= 0) {
        timrStr += `${item}`;
        if (index < timeArr.length - 1) {
          timrStr += yearSplit;
        }
      }
    } else {
      if (index > 0) {
        timrStr += `${item}`;
        if (index < timeArr.length - 1) {
          timrStr += yearSplit;
        }
      }
    }
  });
  let removeSecondTimeStr = "";
  const removeSecondArr = timrStr.split(":");
  for (var i = 0; i < removeSecondArr.length - 1; i++) {
    removeSecondTimeStr += removeSecondArr[i];
    if (i < removeSecondArr.length - 2) {
      removeSecondTimeStr += ":";
    }
  }
  return removeSecondTimeStr;
};

// 获取当前聊天时间
export const getCurrentChatTime = () => {
  // 创建一个 Date 对象
  var today = new Date();
  // 获取年、月、日、时、分、秒
  var year = today.getFullYear();
  var month = today.getMonth() + 1; // 月份是从 0 开始计数的，需要加1
  var day = today.getDate();
  var hours = today.getHours();
  var minutes = today.getMinutes();
  var seconds = today.getSeconds();

  // 格式化输出
  var formattedTime =
    year +
    "-" +
    (month < 10 ? "0" : "") +
    month +
    "-" +
    (day < 10 ? "0" : "") +
    day +
    " " +
    (hours < 10 ? "0" : "") +
    hours +
    ":" +
    (minutes < 10 ? "0" : "") +
    minutes +
    ":" +
    (seconds < 10 ? "0" : "") +
    seconds;

  return getChatTime({ createdAt: formattedTime });
};

// 获取聊天内容
export const getChatContent = (data: any) => {
  if (data.contentFlag === 2) {
    if (data.answerWay === 3) {
      if (isJSON(data.answerContent)) {
        const answerContent = JSON.parse(data.answerContent || "{}");
        return answerContent.text || "";
      } else {
        return data.answerContent || "";
      }
    }
    // 系统
    return data.answerContent || "";
  } else {
    if (isJSON(data.answerContent)) {
      const answerContent = JSON.parse(data.answerContent || "{}");
      return answerContent.text || "";
    } else {
      return data.answerContent || "";
    }
  }
};

// 获取视频封面
export const getChatVideoPoster = (data: any) => {
  if (data.contentFlag === 1) {
    if (isJSON(data.answerContent)) {
      const answerContent = JSON.parse(data.answerContent || "{}");
      if (answerContent.type === "video") {
        return answerContent.firstPic || "";
      }
    } else {
      return "";
    }
  }
  return "";
};

// 获取聊天音频时长 s
export const getChatAudioDuration = (data: any) => {
  if (data.contentFlag === 1) {
    if (isJSON(data.answerContent)) {
      const answerContent = JSON.parse(data.answerContent || "{}");
      if (answerContent.type === "audio") {
        return answerContent.duration || 0;
      }
    } else {
      return 0;
    }
  }
  return 0;
};

// 获取聊天类型
export const getChatType = (data: any) => {
  if (data.contentFlag === 2) {
    //answerWay 回复方式1：人工回复，2：试题推送回复，3：ai回复
    if (data.answerWay === 3) {
      if (isJSON(data.answerContent)) {
        const answerContent = JSON.parse(data.answerContent || "{}");
        if (answerContent.type === "img") {
          // 图片
          return KKConversationType.img;
        }
      }
      // 拍搜AI回复试题 通过试题卡片来展示
      return KKConversationType.exam;
    } else if (data.answerWay === 2) {
      if (data.questionIds && data.questionIds.length > 0) {
        return KKConversationType.exam;
      }
    }
    // 系统
    return KKConversationType.text;
  } else {
    if (isJSON(data.answerContent)) {
      const answerContent = JSON.parse(data.answerContent || "{}");
      if (answerContent.type === "text") {
        // 文本
        return KKConversationType.text;
      } else if (answerContent.type === "richText") {
        // 富文本
        return KKConversationType.richText;
      } else if (answerContent.type === "img") {
        // 图片
        return KKConversationType.img;
      } else if (answerContent.type === "audio") {
        // 音频
        return KKConversationType.audio;
      } else if (answerContent.type === "video") {
        // 视频
        return KKConversationType.video;
      }
    }
  }
  if (data.questionIds && data.questionIds.length > 0) {
    return KKConversationType.exam;
  }

  return KKConversationType.text;
};

// 获取聊天类型通过回复内容
export const getChatTypeByAnswerContent = (content: string) => {
  if (isJSON(content)) {
    const answerContent = JSON.parse(content || "{}");
    if (answerContent.type === "text") {
      // 文本
      return KKConversationType.text;
    } else if (answerContent.type === "richText") {
      // 富文本
      return KKConversationType.richText;
    } else if (answerContent.type === "img") {
      // 图片
      return KKConversationType.img;
    } else if (answerContent.type === "audio") {
      // 音频
      return KKConversationType.audio;
    } else if (answerContent.type === "video") {
      // 视频
      return KKConversationType.video;
    }
  }

  return "";
};

//获取发送的内容
export const getAnswerContent = (data: any) => {
  if (data.chatType === KKConversationType.exam) {
    return JSON.stringify({});
  }
  const answerContent = {
    type: data.chatType,
    text: data.text,
  } as any;
  if (data.chatType === KKConversationType.video) {
    answerContent.firstPic = data.videoPoster;
  } else if (data.chatType === KKConversationType.audio) {
    answerContent.duration = data.duration || 0;
  }
  let answerContentJsonStr = JSON.stringify(answerContent);
  // answerContentJsonStr = answerContentJsonStr.replace(/\\\\/g, "\\");
  return answerContentJsonStr;
};

// 获取试题的类型值
// 1-单选, 2-多选，3-不定项选择题，4-填空题, 5-判断题 6-问答题 7-材料题   ai 1-3选择题

export const getPtypeValueTitle = (value: number, isAi = false) => {
  if (isAi && value >= 1 && value <= 3) {
    return "选择题";
  }
  if (value === 1) {
    return "单选题";
  } else if (value === 2) {
    return "多选题";
  } else if (value === 3) {
    return "不定项选择题";
  } else if (value === 4) {
    return "填空题";
  } else if (value === 5) {
    return "判断题";
  } else if (value === 6) {
    return "问答题";
  } else if (value === 7) {
    return "材料题";
  }
  return "";
};

// 获取AI回复的试题
export const getChatAiExamList = (data: any) => {
  const list = [] as any;
  if (data.contentFlag === 2) {
    //answerWay 回复方式1：人工回复，2：试题推送回复，3：ai回复
    if (data.answerWay === 3) {
      if (isJSON(data.answerContent)) {
        const answerContent = JSON.parse(data.answerContent || "[]") || [];
        answerContent.forEach((item: any) => {
          const examObj = {
            analysis: item.analysis || "",
            isSourceAi: data.answerWay === 3 ? 1 : 0,
            answer: item.answer || [],
            knowledgePointList: item.knowledgePointList || [],
            metas: item.metas || [],
            ptypeValueId: item.ptypeValueId || "",
            ptypeTitle: getPtypeValueTitle(
              Number(item.ptypeValueId || ""),
              true
            ),
            questionTagList: item.questionTagList || [],
            stem: item.stem || "",
            aiReplayHtml: "",
          };
          list.push(examObj);
        });
      } else {
        if (data.answerContent) {
          const examObj = {
            aiReplayHtml: data.answerContent || "",
          };
          list.push(examObj);
        }
      }
    }
  }
  return list;
};

// 替换符文标签中img标签为[图片]
export const replaceImgTagWithText = (
  htmlContent: string,
  replaceStr = "[图片]"
) => {
  return htmlContent.replace(/<img[^>]*>/gi, replaceStr);
};
